<template>
  <div class='container'>
    <el-card>
      <!-- 筛选 -->
      <el-row>
        <el-col :span="18">
          <el-form label-width="80px" size="small" :inline="true">
            <el-form-item label="关键字">
              <el-input style="200px" v-model="form.keyword"></el-input>
           </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="form.state">
                <el-option :value="1" label="启用"></el-option>
                <el-option :value="0" label="禁用"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item style="text-align:right">
              <el-button @click="clearForm">清除</el-button>
              <el-button type="primary" @click="search">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6" style="text-align:right">
          <el-button icon="el-icon-edit" type="success" size="small" @click="showDialog">新增技巧</el-button>
        </el-col>
      </el-row>
      <!-- 提示文本 -->
      <el-alert
        :title="`数据一共 ${total} 条`"
        style="margin-bottom:15px"
        type="info"
        class="alert"
        :closable="false"
        show-icon
      ></el-alert>
      <el-table
        stripe
        border
        ref="singleTable"
        :data="articleList"
        highlight-current-row
        style="width: 100%">
        <el-table-column type="index" width="80px" label="序号"></el-table-column>
        <el-table-column prop="title" label="文章标题" width="450px">
          <template #default="{row}">
            {{row.title}}
            <a @click.prevent="videoShow(row.videoURL)" v-if="row.videoURL" href="#" class="el-icon-film"></a>
          </template>
        </el-table-column>
        <el-table-column prop="visits" label="阅读数" width="200px"></el-table-column>
        <el-table-column prop="username" label="录入人" width="200px"></el-table-column>
        <el-table-column label="录入时间">
          <template slot-scope="scope">{{scope.row.createTime|parseTimeByString}}</template>
        </el-table-column>
        <el-table-column label="状态" width="200px">
          <template slot-scope="scope">{{scope.row.state===1?'已启用':'已禁用'}}</template>
        </el-table-column>
        <el-table-column label="操作" width="200px">
          <template #default="{row}">
            <el-button type="text" @click="previewArticle(row)" >预览</el-button>
            <el-button type="text" @click="toggleState(row.id,row.state)" >{{row.state===1?'禁用':'启用'}}</el-button>
            <el-button type="text" :disabled="row.state===1" @click="showEditDialog(row)">修改</el-button>
            <el-button type="text" :disabled="row.state===1||row.totals > 0" @click="delArt(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="form.page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="form.pagesize"
        layout="prev, pager, next, sizes, jumper"
        :total="total"
        background>
      </el-pagination>
    </el-card>
    <!-- 预览对话框 -->
    <ArticlesPreview :previewVisible.sync="previewVisible" :articleInfo="articleInfo"></ArticlesPreview>
    <!-- 添加、修改对话框 -->
    <ArticlesAdd
    ref="editRef"
    :dialogVisible.sync="dialogVisible"
    :title="title"
    @reload="getArticleList" ></ArticlesAdd>
    <!-- 视频 -->
    <div class="videoBox" v-if="videoURL">
      <div class="close" @click="closeVideo"><span class="el-icon-close"></span></div>
      <div class="box">
        <video ref="video" :src="videoURL"></video>
      </div>
    </div>
  </div>
</template>

<script>
import { list, remove, changeState } from '@/api/hmmm/articles.js'
import ArticlesPreview from '@/module-hmmm/components/articles-preview'
import ArticlesAdd from '@/module-hmmm/components/articles-add'
export default {
  name: 'Articles',
  components: {
    ArticlesAdd,
    ArticlesPreview
  },
  data () {
    return {
      form: {
        keyword: null,
        state: null,
        page: 1,
        pagesize: 10
      },
      total: 0,
      articleList: [],
      videoURL: '',
      previewVisible: false,
      dialogVisible: false,
      articleInfo: {},
      title: '新增文章'
    }
  },
  created () {
    this.getArticleList()
  },
  methods: {
    // 获取文章列表
    async getArticleList () {
      const { data } = await list(this.form)
      // console.log(data)
      this.total = data.counts
      this.articleList = data.items
    },
    // pagesize改变
    handleSizeChange (newPagesize) {
      this.form.pagesize = newPagesize
      this.getArticleList()
    },
    // page改变
    handleCurrentChange (newPage) {
      this.form.page = newPage
      this.getArticleList()
    },
    // 清空表单
    clearForm () {
      this.form = {
        keyword: null,
        state: null,
        page: 1,
        pagesize: 10
      }
    },
    // 删除
    async delArt (id) {
      const str = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        type: 'warning'
      }).catch(() => {})
      if (str !== 'confirm') return this.$message.info('已取消')
      await remove({ id })
      this.getArticleList()
    },
    // 启用禁用切换
    async toggleState (id, state) {
      await changeState({ id, state: state === 1 ? 0 : 1 })
      this.getArticleList()
    },
    // 显示视频的盒子并播放视频
    videoShow (videoURL) {
      this.videoURL = videoURL
      this.$nextTick(() => {
        this.$refs.video.play()
      })
    },
    // 暂停视频播放，关闭视频框
    closeVideo () {
      this.$refs.video.pause()
      this.videoURL = null
    },
    // 预览对话框
    previewArticle (row) {
      this.articleInfo = row
      this.previewVisible = true
    },
    // 打开增加对话框
    showDialog () {
      this.title = '新增文章'
      this.dialogVisible = true
    },
    // 打开修改对话框
    showEditDialog (row) {
      this.title = '修改文章'
      console.log(row)
      this.$refs.editRef.articleForm = row
      this.dialogVisible = true
    },
    // 搜索功能
    search() {
      this.form.page = 1
      this.getArticleList()
    }
  }
}
</script>

<style scoped lang='scss'>
.container{
  margin: 20px 10px;
}
.el-pagination{
  float: right;
  margin: 10px 0;
}
.videoBox{
  position: fixed;
  top: 0;
  width: 100%;
  height: 100%;
  background:rgba(0,0,0,0.3);
  .close{
    width: 50px;
    height: 50px;
    position: absolute;
    top: 130px;
    left: 50%;
    transform: translate(-50%,0);
    background: rgba(0,0,0,0.4);
    border-radius: 50%;
    text-align: center;
    line-height: 50px;
    color: #fff;
    font-size: 20px;
  }
  .box{
    width: 800px;
    height: 600px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    video{
      width: 100%;
      height: 100%;
    }
  }
}
</style>
